package it.uai.domainsMigrator;
import java.io.File;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.log4j.pattern.LogEvent;

public class Domain {

	private static final Logger logger = Logger.getLogger(Domain.class);

	private String nome;
	private String sourcePath;
	private String ftpServer;
	private String ftpUser;
	private String ftpPassword;
	private String destinationPath;

	private String dbSourceName;
	private String dbSourceHost;
	private String dbSourceUser;
	private String dbSourcePassword;

	private String dbDestinationName;
	private String dbDestinationHost;
	private String dbDestinationUser;
	private String dbDestinationPassword;

	private Boolean processed = false;

	private ArrayList<SingleFile> files = new ArrayList<SingleFile>();

	public Domain(String riga, String basePath) {

		String[] valori = riga.split(",");

		nome = valori[0];
		sourcePath = basePath + valori[1];
		ftpServer = valori[2];
		ftpUser = valori[3];
		ftpPassword = valori[4];
		destinationPath = valori[5];

		File folder = new File(sourcePath);

		listRecursiveFiles(folder);

	}

	public String getNome() {
		return nome;
	}

	public String getFtpServer() {
		return ftpServer;
	}

	public String getFtpUser() {
		return ftpUser;
	}

	public String getFtpPassword() {
		return ftpPassword;
	}

	public String getDestinationPath() {
		return destinationPath;
	}

	public Boolean isProcessed() {
		return processed;
	}

	public synchronized void setProcessed() {
		this.processed = true;
		notifyAll();
	}

	public ArrayList<SingleFile> getFiles() {
		return files;
	}

	public String getSourcePath() {
		return sourcePath;
	}

	public ArrayList<SingleFile> getFiles(Integer number) {

		ArrayList<SingleFile> limitedFiles = new ArrayList<SingleFile>();

		for (SingleFile file : files) {
			if (file.isRegularFile() && !file.isAssignToFtpProcess()) {
				file.setAssignToFtpProcess();
				limitedFiles.add(file);
			}
			if (limitedFiles.size() >= number) {
				break;
			}
		}
		return limitedFiles;

	}

	public ArrayList<SingleFile> getDirectories() {

		ArrayList<SingleFile> directories = new ArrayList<SingleFile>();

		for (SingleFile file : files) {
			if (!file.isRegularFile()) {
				directories.add(file);
			}
		}
		return directories;

	}

	private void listRecursiveFiles(File file) {

		for (File f : file.listFiles()) {

			SingleFile singolo = new SingleFile(f);
			files.add(singolo);
			if (f.isDirectory()) {
				singolo.setDirectory();
				listRecursiveFiles(f);
			}
		}
	}

	private Integer countItem(Boolean tipo) {

		Integer contatoreFile = 0;
		Integer contatoreDirectory = 0;

		for (SingleFile file : files) {
			if (file.isRegularFile()) {
				contatoreFile++;
			} else {
				contatoreDirectory++;
			}
		}

		if (tipo) {
			return contatoreFile;
		} else {
			return contatoreDirectory;

		}

	}

	public Integer countFile() {

		return countItem(true);

	}

	public Integer countDirectory() {

		return countItem(false);

	}
	
	private Integer countNoProcessedItems(){
		
		Integer contatore = 0;
		
		for (SingleFile file : files) {
			if (file.isRegularFile() && !file.isTransfered()) {
				contatore++;
			}
		}
		
		return contatore;
		
	}
	
	private Integer countNoAssignedItems(){
		
		Integer contatore = 0;
		
		for (SingleFile file : files) {
			if (file.isRegularFile() && !file.isAssignToFtpProcess()) {
				contatore++;
			}
		}
		
		return contatore;
		
	}
	

	public Boolean existItemNoAssigned() {

		if(countNoAssignedItems()>0){
			return true;
		}else{
			return false;			
		}

	}
	
	public void printStatistics(){
		
		Integer totale = countFile();
		Integer noProcesed = countNoProcessedItems();
		Integer processati = totale - noProcesed;
		Integer percentuale = processati * 100 / totale;
		
		logger.info("File processati: " + processati + "/" + countFile() + " - " + percentuale + "%");
		
	}

}
